﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ThuVien;

namespace TranTekBE.Price
{
    public partial class Cap_Nhat_Gia_San_Pham_Website : PageRequireLogin
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Load JS
            ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "LoadJS", "LoadJS();", true);
            if (!IsPostBack)
            {
                lb_time.Text = DateTime.Now.ToShortDateString();
                LoadData();
            }
        }

        
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            DateTime get_date_now = DateTime.Now;
            string format_date = get_date_now.ToString("yyyy-MM-dd");
            //string format_date = "2014-06-20";
            //
            ConnectMysql connect = new ConnectMysql();
            //
            string sql = @"
            select sp.C001200 as ID, sp.C001203 as MaPOS, sp.C001204 as TenSP,
            gia.UC00003 as NgayCapNhat  from T0007 gia 
            Left Join T0012 sp on sp.C001200 = gia.C000701
            where
            gia.UC00003 >= '" + format_date + @" 00:00:00.001' 
            and gia.UC00003 <= '" + format_date + @" 23:59:59.999'
            Group by sp.C001200,sp.C001203, sp.C001204,gia.UC00003
            order by gia.UC00003 desc
            ";
            DataSet ds = FunctionsUtils.Database.ExecuteQuery(sql);

            DataTable dt = ds.Tables[0];
            int UpdateSP = 0;
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i <= dt.Rows.Count; i++)
                {
                    try
                    {
                        string ID = dt.Rows[i]["ID"].ToString();
                        string MaPOS = dt.Rows[i]["MaPOS"].ToString();

                        if (ID != null && ID != "")
                        {
                            string sql_price = @"select top 1 C000701 as ID, C000704 as Gia 
                                                from T0007 
                                                where C002000 =3 and C000701 = '" + ID + @"' order by C000704 desc";
                            DataSet ds_price = FunctionsUtils.Database.ExecuteQuery(sql_price);
                            if (ds_price != null && ds_price.Tables.Count > 0 && ds_price.Tables[0].Rows.Count > 0)
                            {
                                DataTable dt_price = ds_price.Tables[0];
                                string GiaSP = dt_price.Rows[0]["Gia"].ToString();
                                if (GiaSP != "" && GiaSP != null && GiaSP != "0")
                                //
                                {
                                    string mysql = "UPDATE product SET price='" + GiaSP + "',date_update='" + format_date + "' WHERE pos_id = '" + MaPOS + "'";
                                    UpdateSP = connect.QueryData(mysql);
                                }
                            }
                        }
                        
                    }
                    catch
                    { }

                }
                if (UpdateSP >= 1)
                {
                    Response.Write("<script>alert('Bạn đã Update giá thành công!');</script>");
                }
                else
                {
                    Response.Write("<script>alert('Bạn đã Update giá thất bại, vui lòng thử lại!');</script>");
                }
            }


        }
        void LoadData()
        {
            DateTime get_date_now = DateTime.Now;
            string format_date = get_date_now.ToString("yyyy-MM-dd");
            //string format_date = "2014-06-20";
            //
            string sql = @"
            select sp.C001200 as ID, sp.C001203 as MaPOS, sp.C001204 as TenSP,
            gia.UC00003 as NgayCapNhat  from T0007 gia 
            Left Join T0012 sp on sp.C001200 = gia.C000701
            where
            gia.UC00003 >= '" + format_date + @" 00:00:00.001' 
            and gia.UC00003 <= '" + format_date + @" 23:59:59.999'
            Group by sp.C001200,sp.C001203, sp.C001204,gia.UC00003
            order by gia.UC00003 desc
            ";
            
            DataSet ds = FunctionsUtils.Database.ExecuteQuery(sql);

            DataTable dt = ds.Tables[0];

            PagedDataSource pgitems = new PagedDataSource();

            System.Data.DataView dv = new System.Data.DataView(dt);

            pgitems.DataSource = dv;

            pgitems.AllowPaging = true;

            pgitems.PageSize = 10;

            pgitems.CurrentPageIndex = PageNumber;

            if (pgitems.PageCount > 1)
            {

                rptPages.Visible = true;

                System.Collections.ArrayList pages = new System.Collections.ArrayList();

                for (int i = 0; i < pgitems.PageCount; i++)

                    pages.Add((i + 1).ToString());

                rptPages.DataSource = pages;

                rptPages.DataBind();

            }

            else

                rptPages.Visible = false;

            rpDanhGia.DataSource = pgitems;

            rpDanhGia.DataBind();

        }

        public int PageNumber
        {

            get
            {

                if (ViewState["PageNumber"] != null)

                    return Convert.ToInt32(ViewState["PageNumber"]);

                else

                    return 0;

            }

            set
            {

                ViewState["PageNumber"] = value;

            }

        }

        protected void rptPages_ItemCommand(object source, RepeaterCommandEventArgs e)
        {

            PageNumber = Convert.ToInt32(e.CommandArgument) - 1;

            LoadData();

        }
    }
}